package com.jobanalysis.job_analysis.controller;

import com.jobanalysis.job_analysis.service.IDataTransferService;
import com.jobanalysis.job_analysis.service.IJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class ExcelUploadController {

    @Autowired
    private IDataTransferService dataTransferService;

    @Autowired
    private IJobService iJobService;

    /**
     * Excel文件上传
     *
     * @param file：文件
     * @param ：文件名
     * @param path:上传到的HDFS路径
     * @param id：所属分类
     * @return
     */
    @RequestMapping("/upload")
    public Map uploadExcelFile(@RequestParam("file") MultipartFile file, String path, Integer id) {
        Map map = new HashMap();
        if (file.isEmpty()) {
            map.put("msg", "文件为空");
            return map;
        }
        int num = iJobService.countJobSortByPathClass(path, id);
        if (num == 0) {
            map.put("msg", "路径与id填写错误");
            return map;
        }


        String response = dataTransferService.transferExcelToHDFS(file, path, id);
        map.put("msg", response);
        return map;
    }

    @PostMapping("/uploadMore")
    public Map uploadExcelFiles(@RequestParam("file") MultipartFile[] file, String path, Integer id) {
        Map map = new HashMap();
        if (file == null || file.length == 0) {
            map.put("msg", "文件为空");
            return map;
        }
        int num = iJobService.countJobSortByPathClass(path, id);
        if (num == 0) {
            map.put("msg", "路径与id填写错误");
            return map;
        }
        String response = new String();
        for (MultipartFile fi : file) {
            response = dataTransferService.transferExcelToHDFS(fi, path, id);
        }
        map.put("msg", response);
        return map;
    }
}
